require([ 'jquery', 'common', 'validate','timepicker', 'pagination', 'bootstrap', 'dateZh','plupload','T_upload'], function($, common,validate,timepicker,T_upload) {
	
	//日期控件
	$('#date-start, #date-end').datetimepicker({
		format : "yyyy-mm-dd hh:ii:ss",
		language : "zh-CN",
		todayBtn : 1,
		autoclose : true,
		todayHighlight : true,
		forceParse : true,
		minuteStep : 10
	});
	
	var addForm = $('#addForm');
	var updateForm = $('#updateForm');
	var addFreightTemplateForm = $('#addFreightTemplateForm');
	var updateFreightTemplateForm = $('#updateFreightTemplateForm');
	
	
	var first_load = true;
	var reload = true; 
	
	$(document).ready(function() {
		loadOptDetail(); 
		showWarehouse();
		showFreightTemplate();
		$("input.checkoneblock").each(function(index,one){
			if(one.checked==true){
				console.log("***");
				console.log(one);
				$(one).find("input.oneopt").attr("checked","checked");
			}
		});
	});
	
	//placeholder兼容ie start
	$(function() {
		if (!placeholderSupport()) { // 判断浏览器是否支持 placeholder
			$('[placeholder]').focus(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
					input.removeClass('placeholder');
				}
			}).blur(function() {
				var input = $(this);
				if (input.val() == ''
						|| input.val() == input.attr('placeholder')) {
					input.addClass('placeholder');
					input.val(input.attr('placeholder'));
				}
			}).blur();
		};
		
		showByPage(1); 
	});
	function placeholderSupport() {
		return 'placeholder' in document.createElement('input');
	}
	//placeholder兼容ie end

	//清除按钮
	$('.clearBtn').on('click', function() {
		$('.setTableLength').val(10);
		$("#freighttemplateName").val(''), 
		$("select[name='status']").val('')
	});
	
	//全选当前页
	$('.checknow').on('click', function() {
		$.each($("#myTabContent>div.active").find("input.oneopt"),function(index,oneli){
			oneli.checked=true;
		});
		
		$.each($("#myTabContent>div.active").find("input.checkoneblock"),function(index,oneli){
			oneli.checked=true;
		});
	});
	
	//全否当前页
	$('.nothingnow').on('click',function(){
		$.each($("#myTabContent>div.active").find("input.oneopt"),function(index,oneli){
			oneli.checked=false;
		});
		
		$.each($("#myTabContent>div.active").find("input.checkoneblock"),function(index,oneli){
			oneli.checked=false;
		});
	});
	
	//全选所有
	$('.all').on('click',function(){
		$.each($("input.oneopt"),function(index,oneli){
			oneli.checked=true;
		});
		$.each($("input.checkoneblock"),function(index,oneli){
			oneli.checked=true;
		});
	});
	
	//全否所有
	$('.nothing').on('click',function(){
		$.each($("input.oneopt"),function(index,oneli){
			oneli.checked=false;
		});
		$.each($("input.checkoneblock"),function(index,oneli){
			oneli.checked=false;
		});
	});
	
	//每页中一小块全选
	$(document).on("click","input.checkoneblock",function(){
		var ret = this.checked;
		$("input.checkoneblock").each(function(index,one){
			$(one).change(function(){
				$(one).parents("li").find("input.oneopt").prop("checked",ret);
			});
		});
	});
	
	$(".setTableLength").change(function() {
		reload = true;
		showByPage(1);
	});
	
	//添加运费模板
	$("#btn_add_FreightTemplate").click(function(e){
		var isValid = addFreightTemplateForm.valid();
		var url; 
        if (isValid) { 
    		url = ctx + '/freight/template/save'; 
        	 $.post(url, $('#addFreightTemplateForm').serialize(),function (data) {
         		if(data.result!=null && data.result=="againLogin"){
	         		$('.commonConfirm').modal('hide');
	         		common.alert1("登录过期，请重新登录");
					setTimeout("location.href = ctx + '/login.html'",1000);
            	}else if(data.result=='success'){
            		$('.modal-add').modal('hide'); 
            		common.mySuccessModal2('提交成功'); 
	      	        $("#addFreightTemplateForm").find("input[name='warehouseCode']").val('');
	      			$("#addFreightTemplateForm").find("input[name='warehouseName']").val(''); 
	      			$("#addFreightTemplateForm").find("input[name='address']").val('');
		      	    $("#addFreightTemplateForm").find("input[name='remark']").val('');  
              	}else if(data.result=='fail'){
              		$(".errorMsg").html(data.message);
              	}else{
              		$(".errorMsg").html("信息提交错误");
              	}
              },"json"); 
        	
        }
	});

	$(".searchBtn").click(function() {
		reload = true;
		showByPage(1);
	});
	
	
	//加载后台用户可操作权限
	function loadOptDetail(freightId) {
		
		//全否所有
		$.each($("input.checkoneblock"),function(index,oneli){
			oneli.checked=false;
		});
		
		$("#myTabContent").html(""); 
		$('#myTab').html(""); 
		if ($(".all").is(':checked')) {
			$(".all").removeAttr('checked');								
		}	
		$("#dataBody").append(loadingHtml);
		if($('.alert-warning').length>0){
    		$('.alert-warning').remove();
    	}
		var url;
		url = ctx + '/freight/querybyid'; 
		$.getJSON(url, {freightId : freightId},
			function(data){ 
				if(data.provinceList!=null && data.provinceList=="againLogin") {
					if($('.loading')){
						$('.loading').remove();
					} 
					common.alert1("登录过期，请重新登录");
					setTimeout("location.href = ctx + '/login.html'",1000);
       			} else if(data.provinceList.length == 0) { 
       				if($('.loading')){
    					$('.loading').remove();
    				} 
					$('.table-responsive').append('<div class="alert alert-warning no-result text-center" role="alert">没有找到记录 </div>');
                } else{
       				var provinceList = data.provinceList; 
       				//已存在的城市id 
       				var selectedDistrictIds = data.selectedDistrictIds;
       				console.log("selectedDistrictIds:" + selectedDistrictIds);
                    var tempHtml="";    
                    
                    var freight = data.freight; 
                    if(freight!=null){
                    	$("#addForm").find("input[name='id']").val(freight.id);
            			$('#addForm').find("input[name='freightName']").val(freight.freightName);
            			$('#addForm').find("select[name='firstHeavy']").val(freight.firstHeavy); 
            			$('#addForm').find("input[name='firstHeavyFreight']").val(freight.firstHeavyFreight);
            			$('#addForm').find("input[name='otherHeavyFreight']").val(freight.otherHeavyFreight);
            			$('#addForm').find("input[name='remark']").val(freight.remark);
            			$('#addForm').find("select[name='warehouseId']").val(freight.warehouseId);
            			$('#addForm').find("select[name='freightTemplateId']").val(freight.freightTemplateId);
                    }
                    
                    for(var i=0;i<provinceList.length;i++){   
                    	var province = provinceList[i];
                    	tempHtml += "<li class=\"\"><a href=\"#"+province.id+"\" data-toggle=\"tab\">"+province.name+"</a></li>"; 
                    	tempHtml +="</tr>";
                    	if($('.alert-warning').length>0){
                    		$('.alert-warning').remove();
                    	}
                    } 
                    $('#myTab').html(tempHtml); 
                    
                    for(var i=0;i<provinceList.length;i++){
                    	var addHtml = "";
                    	var oneli = "";
                    	var cityList = provinceList[i].cityList;
                    	for(var j=0;j<cityList.length;j++){
                    		var city = cityList[j];
                    		var optHtml = "";
                    		var districtList = city.districtDto;
                    		for(var k=0;k<districtList.length;k++){
                    			var opt = districtList[k]; 
                    			if(selectedDistrictIds.indexOf(","+opt.id+",")!=-1){
                    				optHtml += "<li><input type=\"checkbox\" class=\"oneopt\" value=\""+opt.id+"\" checked=\"checked\" /><span>"+opt.name+"</span></li>";
                    			}else{
                    				optHtml += "<li><input type=\"checkbox\" class=\"oneopt\" value=\""+opt.id+"\" /><span>"+opt.name+"</span></li>";
                    			}
                    		}
                    		var choose = city.choosedStr + "";
                    		if(choose.indexOf(","+city.id+",")!=-1){
                    			oneli += "<li><input type=\"checkbox\" class=\"checkoneblock\" value=\""+city.id+"\" checked=\"checked\" /><span style='font-size:large'>"+city.name+"</span>" +
                    					 "<div class=\"vip_details\"><ul>"+ optHtml +"</ul></div></li>";
                    		}else{
                    			oneli += "<li><input type=\"checkbox\" class=\"checkoneblock\" value=\""+city.id+"\" /><span style='font-size:large'>"+city.name+"</span><div class=\"vip_details\">"+
                    					 "<ul>"+ optHtml +"</ul></div></li>";
                    		}
                    	}
                    	if(i==0){
                    		addHtml += "<div class=\"tab-pane fade in active\" id=\""+provinceList[i].id+"\"><ul>"+oneli+"</ul></div>";
                    	} else{
                    		addHtml += "<div class=\"tab-pane fade in\" id=\""+provinceList[i].id+"\"><ul>"+oneli+"</ul></div>";
                    	}
                    	$("#myTabContent").append(addHtml);
                    } 
                    if($('.loading')){
    					$('.loading').remove();
    				} 
				} 
			}).fail(function (err){ 
				if($('.loading')){
					$('.loading').remove();
				}
				common.alert('服务器错误！请刷新页面重试或联系管理员',"确定"); 
            });  
	}
	
	/*加载所有运费模板信息s*/
	function showFreightTemplate(){
		 $.post(ctx+"/freight/template/list", {},function (data) {
			var result = data.result;
			if(result!=null && result=="noauthority") {
				common.alert1('你无此权限!');
				return;
			}
     		if(data.result!=null && data.result=="againLogin") {
         		$('.commonConfirm').modal('hide');
     			common.alert1("登录过期，请重新登录");
				setTimeout("location.href = ctx + '/login.html'",1000);
        	}else{
        		var datalist = data.list;
        		var tempHtml = "";
        		for(var i=0;i<datalist.length;i++){   
                	var data = datalist[i];
                	tempHtml += "<option value='"+data.id+"'>"+data.templateName+"</option>";
                }
      	        $("#addForm").find("select[name='freightTemplateId']").html(tempHtml); 
          	}
          },"json");
	}
	/*加载所有仓库信息e*/
	
	/*加载所有仓库信息s*/
	function showWarehouse(){
		 $.post(ctx+"/warehouse/list", {},function (data) {
			var result = data.result;
			if(result!=null && result=="noauthority") {
				common.alert1('你无此权限!');
				return;
			}
     		if(data.result!=null && data.result=="againLogin") {
         		$('.commonConfirm').modal('hide');
     			common.alert1("登录过期，请重新登录");
				setTimeout("location.href = ctx + '/login.html'",1000);
        	}else{
        		var datalist = data.list;
        		var tempHtml = "";
        		for(var i=0;i<datalist.length;i++){   
                	var data = datalist[i];
                	tempHtml += "<option value='"+data.id+"'>"+data.warehouseName+"</option>";
                }
      	        $("#addForm").find("select[name='warehouseId']").append(tempHtml); 
          	}
          },"json");
	}
	/*加载所有仓库信息e*/
	
	
	/** 列表查询start **/
	function showByPage(pageNum) {
		if ($(".all").is(':checked')) {
			$(".all").removeAttr('checked');								
		}	
		if($('.alert-warning').length>0){
    		$('.alert-warning').remove();
    	}
		$.getJSON(ctx + '/freight/showbypage', {	
				pageNum : pageNum,
				pageSize : $('.setTableLength').val() || 10, 
				freighttemplateName : $("#freighttemplateName").val(), 
				status : $("select[name='status']").val()
			},
			function(data){  
				if($('.loading')){
					$('.loading').remove();
				}
				var result = data.result;
				if(result!=null && result=="noauthority") {
					common.alert1('你无此权限!');
					return;
				}
				if(data.result!=null && data.result=="againLogin") {
					common.alert1("登录过期，请重新登录");
						setTimeout("location.href = ctx + '/login.html'",1000);
       			} else if(data.result.length == 0) { 
					$('.table-responsive').append('<div class="alert alert-warning no-result text-center" role="alert">没有找到记录 </div>');
                } else{
       				var datapage = data.result;
					var datalist = datapage.result;
					var totalCount = datapage.totalCount;
					var pageNum = datapage.pageNo;
					var pageSize = datapage.pageSize;
					var totalPages = datapage.totalPages;
					$(".totalCount").html(totalCount);
					$(".totalPages").html(totalPages);
					$(".pageNo").html(pageNum);
                    var tempHtml="";   
                    var ser = 1;
                    
                    for(var i=0;i<datalist.length;i++){   
                    	var productList = datalist[i]; 
                    	var freightRegionList = productList.freightRegionList; 
                    	var regionHtm = "";
                    	if(freightRegionList != null && freightRegionList.length > 0) { 
                    		var regionSize = freightRegionList.length; 
                    		console.log("regionSize:" + regionSize);
                    		var maxSize = 4;	//页面最多显示地区数量
                    		var n = regionSize > maxSize ? maxSize : regionSize; 
                    		for(var m=0;m<n;m++) {
                    			regionHtm += freightRegionList[m].pdcName;
                    			if(m != (maxSize-1)){
                    				regionHtm += "、";
                    			}
                    		}
                    		if(regionSize < maxSize){
                    			regionHtm = regionHtm.substring(0, regionHtm.length-1);
                    		} 
                    		if(n == 4){
                    			regionHtm += "等" + freightRegionList.length + "个地区"
                    		}
                    	}
                    	var no = (pageNum-1)*pageSize+i+1; 
                    	tempHtml +="<tr data-id='"+productList.id+"' data-pageno='" + pageNum + "'>";
                    	tempHtml +="<td><div class='checkbox'><label><input type='checkbox' data-roleid="+productList.id+" name='itemCheck' value="+productList.id+" /></label></div></td>";
                    	tempHtml +="<td>"+ser+"</td>"; 
                    	tempHtml +="<td><span style='cursor: pointer;' data-toggle='modal' data-id=" +productList.freightTemplateId+" data-target='.modal-update-freight-template' class='updateFreightTemplate'>"
                    		+isnull(productList.freightTemplateName)+"</span></td>";
                    	tempHtml +="<td>"+isnull(productList.freightName)+"</td>"; 
                    	tempHtml +="<td>"+isnull(productList.warehouseName)+"</td>"; 
                    	tempHtml +="<td>"+regionHtm+"</td>"; 
                    	tempHtml +="<td>"+isnull(productList.firstHeavy)+"</td>";   
                    	tempHtml +="<td>"+isnull(productList.firstHeavyFreight)+"</td>";
                    	tempHtml +="<td>"+isnull(productList.otherHeavyFreight)+"</td>"; 
                    	tempHtml +="<td>"+common.getSmpFormatDate(productList.createTime)+"</td>"; 
                    	tempHtml +="<td>"+isnull(productList.remark)+"</td>";
                    	tempHtml +="<td>";
                    		tempHtml +="<button type='button' data-toggle='modal' data-id=" +productList.id+" data-target='.modal-add-freight-region' class='btn btn-warning btn-xs updatefreight'>编辑</button>&nbsp";
                    	tempHtml +="</td>";
                    	ser = ser + 1;
                    	tempHtml +="</tr>";
                    	if($('.alert-warning').length>0){
                    		$('.alert-warning').remove();
                    	}
                    }   
                    if(datalist.length === 0&&$('.alert-warning').length<=0) { 
						$('.table-responsive').append('<div class="alert alert-warning no-result text-center" role="alert">没有找到记录 </div>');
	                } 
                    $('#dataBody').html(tempHtml);
                   
                    if (reload) {
                    	reload = false;
                    	first_load = true;
    					var initPagination = function() {
    						$("#Pagination").pagination(totalCount, {
    							num_edge_entries : 1, // 边缘页数
    							num_display_entries : 4, // 主体页数
    							callback: pageselectCallback,
    							prev_text : "«",
    							next_text : "»",
    							items_per_page : pageSize, 
    							link_to : 'javascript:void(0);'
    						});
    					}();
    					/**分页e**/
                    }
				} 
			}).fail(function (err) { 
				if($('.loading')){
					$('.loading').remove();
				}
				common.alert('服务器错误！请刷新页面重试或联系管理员',"确定"); 
            });  
	}
	
	function pageselectCallback(page_index, jq) {
		if (first_load) { //第一次初始化不需要请求
			first_load = false;
			return false;
		}
		pageNum = page_index + 1;
		showByPage(pageNum);
	}
	/** 列表查询end **/
	
	/** 添加验证start **/
	$('#addshopForm').validate({
        rules: {
            'productName': {
                required: true 
            },
            'code': {
                required: true 
            },
            'productType': {
                required: true
            },
            'stock': {
                required: true,
                digits: true,
                min:0
            },
            'weight': {
                number: true
            },
            'price': {
                required: true,
                digits: true,
                number: true
            },
	        'mainImg': {
	        	required: true
	        }
        },
        messages: {
        	'name': {
     		    required: "请填写商品名称" 
     		 },
     		'code': {
                required: "请填写商品编号" 
            },
     		'originalprice': {
                required: "请填写商品市场价格",
                number: "请输入有效的数字"
            },
            'stock': {
            	required: "请填写商品库存数量",
            	digits: "请输入有效的整数",
            	min : "不能小于0"
            },
            'weight': {
                number: "请输入有效的数字"
            },
     		'price': {
     		    required: "请填写商品积分价格",
     		    digits: "请输入有效的整数",
     		    number: "请输入有效的数字"
     		 },
     		'mainImg': {
	        	required: "请选择主图"
	        }
        },
        errorClass: "error",
		success: 'valid',
		unhighlight: function(element, errorClass, validClass) { //验证通过
			$(element).tooltip('destroy').removeClass(errorClass);
		}, 
		errorPlacement: function(label, element) {
			$(element).tooltip('destroy'); /*必需*/
			$(element).attr('title', $(label).text()).tooltip('show');
		},
        onclick: true 
    });
	/** 添加验证end **/
	
	//新增发货地运费
	$("#modal-add-btn").click(function(e){
		
		showFreightTemplate();
		
		$(".updateMsg").html("");
      	$("#addForm").find("input[name='id']").val("");
  		$('#addForm').find("input[name='freightName']").val("");
  		$('#addForm').find("input[name='firstHeavy']").prop('disabled', false); 
  		$('#addForm').find("input[name='firstHeavyFreight']").prop('disabled', false); 
  		$('#addForm').find("input[name='otherHeavyFreight']").prop('disabled', false); 
  		$('#addForm').find("input[name='firstHeavy']").val(""); 
  		$('#addForm').find("input[name='firstHeavyFreight']").val("");
  		$('#addForm').find("input[name='otherHeavyFreight']").val("");
  		$('#addForm').find("input[name='remark']").val("");
  		 
  		//类型，1发货区域，2停发区域
  		$("#addForm").find("input[name='freightType']").val("1"); 
	});
	
	//新增运费模板
	$("#modal-add-freight-template-btn").click(function(e){

	});
	
	//新增停发地区
	$("#modal-addStop-btn").click(function(e){
		$(".updateMsg").html("");
      	$("#addForm").find("input[name='id']").val("");
  		$('#addForm').find("input[name='freightName']").val("");
  		$('#addForm').find("input[name='firstHeavy']").attr("disabled", "disabled"); 
  		$('#addForm').find("input[name='firstHeavyFreight']").attr("disabled", "disabled"); 
  		$('#addForm').find("input[name='otherHeavyFreight']").attr("disabled", "disabled"); 
  		$('#addForm').find("input[name='remark']").val("");
  		 
  		//类型，1发货区域，2停发区域
  		$("#addForm").find("input[name='freightType']").val("2");
  		
  		
	});
	
	
	//添加
	$("#btn_add").click(function(e){
		var isValid = addForm.valid();
		var url;
		//选中的地区
		var values = "";
		$("#addForm").find("input.oneopt").each(function(index,one){
			if(one.checked==true){
				values += "," + one.value ;
			}
		});
		$("#addForm").find("input[name='regionVals']").val(values);
		console.log("选中的地区为：" + values);
		var queryType = $("#addForm").val();
        if (isValid) { 
    		url = ctx + '/freight/save'; 
        	 $.post(url, $('#addForm').serialize(),function (data) {
         		if(data.result!=null && data.result=="againLogin"){
	         		$('.commonConfirm').modal('hide');
	         		common.alert1("登录过期，请重新登录");
					setTimeout("location.href = ctx + '/login.html'",1000);
            	}else if(data.result=='success'){
            		$('.modal-add-freight-region').modal('hide');
            		showByPage(1);
            		common.mySuccessModal2('提交成功'); 
	      	      $(".updateMsg").html("");
		          	$("#addForm").find("input[name='id']").val("");
		      		$('#addForm').find("input[name='freightName']").val("");
		      		$('#addForm').find("input[name='firstHeavy']").val(""); 
		      		$('#addForm').find("input[name='firstHeavyFreight']").val("");
		      		$('#addForm').find("input[name='otherHeavyFreight']").val("");
		      		$('#addForm').find("input[name='remark']").val("");
	      			
              	}else if(data.result=='fail'){
              		$(".errorMsg").html(data.message);
              	}else{
              		$(".errorMsg").html("信息提交错误");
              	}
              },"json"); 
        	
        }
	});
	
	/**上下架start */
	$(document).on('click', '.update-btn', function(){
		var varid = $(this).data("id");
        var state = $(this).data('state');
        var parentTr = $(this).parents('tr');
        var pageno = parentTr.data('pageno');
        if(state == 1){
        	var str = "启用";
		}else if(state == 2){
			var str = "停用";
		}else{
			var str = "--";
		}
		$('.commonConfirm').modal('show');
		common.commonConfirm("修改", "确定"+str+"该快递吗？", "",
			function() {
            	 $.post(ctx+"/freight/updatestatus", {
    				 id:varid,status:state
    			 },function (data) {
    				 var result = data.result;
					 if(result!=null && result=="noauthority") {
						 common.alert1('你无此权限!');
						 return;
					 }
    	    		 if(data.result!=null && data.result=="againLogin") {
    	    			 $('.commonConfirm').modal('hide');
    	    			 common.alert1("登录过期，请重新登录");
    					 setTimeout("location.href = ctx + '/login.html'",1000);
    	             }else if(data.result=='success'){ 
    	            	$('.commonConfirm').modal('hide');
    	     	        common.mySuccessModal2(data.message);
    	     	       showByPage(pageno);
    	         	}else{
    	         		common.alert1(data.message);
    	         	}
    	         },"json"); 
			}
		);
	});
	/** 上下架end **/
	
	/**删除多个s*/
	$('#deleteMany').click(function(e) {
		var ids = "";
		var count = 0;
		var checks = document.getElementsByName("itemCheck");
		console.log(checks);
		for(var i=0;i<checks.length;i++){
			if(checks[i].checked){
				count++;
				ids += checks[i].value + ",";
			}
		}
		if(count<1){
			common.alert1("请至少选择一个");
		}else{
			$('.commonConfirm').modal('show');
			common.commonConfirm("删除", "确定删除", "这"+count+"个吗",
				function() {
	            	 $.post(ctx+"/freight/deletebyids", {ids:ids},
	            		function (data) {
		            		 var result = data.result;
		     				 if(result!=null && result=="noauthority") {
		     					 common.alert1('你无此权限!');
		     					 return;
		     				 }
	    		    		 if(data.result!=null && data.result=="againLogin") {
	    		    			 $('.commonConfirm').modal('hide');
	    		    			 common.alert1("登录过期，请重新登录");
	    						 setTimeout("location.href = ctx + '/login.html'",1000);
	    		             }else if(data.result=='success'){ 
	    		         		 $(".commonConfirm").modal('hide');
	    		     	         common.mySuccessModal2(data.message);
	    		     	        showByPage(1);
	    		         	}else{
	    		         		$(".commonConfirm").modal('hide');
	    		         		common.alert1(data.message); 
	    		         	}
	    		 },"json"); 
			});
		}
	});
	/**删除多个e*/
	
	/** 修改验证start **/
	$('#updateProductForm').validate({
        rules: {
            'name': {
                required: true 
            },
            'code': {
                required: true 
            },
            'originalprice': {
                required: true,
                number: true
            },
            'stock': {
                required: true,
                digits: true,
                min:0
            },
            'weight': {
                number: true
            },
            'price': {
                required: true,
                number: true
            }
        },
        messages: {
        	'name': {
     		    required: "请填写商品名称" 
     		 },
     		'code': {
                required: "请填写商品编号" 
            },
     		'originalprice': {
                required: "请填写商品市场价格",
                number: "请输入有效的数字"
            },
            'stock': {
            	required: "请填写商品库存数量",
            	digits: "请输入有效的整数",
            	min : "不能小于0"
            },
            'weight': {
                number: "请输入有效的数字"
            },
     		'price': {
     		    required: "请填写商品积分价格",
     		    number: "请输入有效的数字"
     		 }
        },
        errorClass: "error",
		success: 'valid',
		unhighlight: function(element, errorClass, validClass) { //验证通过
			$(element).tooltip('destroy').removeClass(errorClass);
		}, 
		errorPlacement: function(label, element) {
			$(element).tooltip('destroy'); /*必需*/
			$(element).attr('title', $(label).text()).tooltip('show');
		},
        onclick: true 
    });
	/** 修改验证end **/
	
$(document).on('click', '.updateFreightTemplate', function (e) { 
        var $this = $(this),
        parentTr = $this.parents('tr'),
        id = $this.data('id'),
        pageno = parentTr.data('pageno');
    	$(".updateMsg").html("");
    	$("#updateFreightTemplateForm").find("input[name='id']").val("");
		$('#updateFreightTemplateForm').find("input[name='templateName']").val("");
		$('#updateFreightTemplateForm').find("select[name='firstHeavy']").val(""); 
		$('#updateFreightTemplateForm').find("input[name='firstHeavyFreight']").val("");
		$('#updateFreightTemplateForm').find("select[name='otherHeavy']").val(""); 
		$('#updateFreightTemplateForm').find("input[name='otherHeavyFreight']").val("");  
		$.getJSON(ctx + '/freight/template/' +id , {
    	}, function(data) {
    		var result = data.result;
			if(result!=null && result=="noauthority") {
				common.alert1('你无此权限!');
				return;
			}
    		if(data==null || data.length <= 0){
    			$(".updateMsg").html("数据异常,请重试");
    		}else{
    			var product = data.freightTemplate; 
    			$("#updateFreightTemplateForm").find("input[name='id']").val(product.id);
    			$('#updateFreightTemplateForm').find("input[name='templateName']").val(product.templateName);
    			$('#updateFreightTemplateForm').find("select[name='firstHeavy']").val(product.firstHeavy);
    			$("#updateFreightTemplateForm").find("input[name='firstHeavyFreight']").val(product.firstHeavyFreight);
    			$('#updateFreightTemplateForm').find("select[name='otherHeavy']").val(product.otherHeavy);
    			$("#updateFreightTemplateForm").find("input[name='otherHeavyFreight']").val(product.otherHeavyFreight);
    		}
    	});
});

/** 修改start **/
$("#btn_update_FreightTemplate").click(function() {
 	var isValid = updateFreightTemplateForm.valid();
     if (isValid) { 	
     	 $.post(ctx+"/freight/template/update", $('#updateFreightTemplateForm').serialize(),function (data) {
     		var result = data.result;
			if(result!=null && result=="noauthority") {
				common.alert1('你无此权限!');
				return;
			}
     		 if(result!=null && result=="againLogin") {
    			 $('.commonConfirm').modal('hide');
    			 common.alert1("登录过期，请重新登录");;
				 setTimeout("location.href = ctx + '/login.html'",1000);
             }else if(result=='success'){ 
          		 $(".modal-update-freight-template").modal('hide');
      	         common.mySuccessModal2('修改成功!'); 
          	}else{
          		$(".updateMsg").html(data.message);
          	}
          },"json"); 
     }  
});
/** 修改end **/
	
$(document).on('click', '.updatefreight', function (e) { 
        var $this = $(this),
        parentTr = $this.parents('tr'),
        id = $this.data('id'),
        pageno = parentTr.data('pageno');
    	$(".updateMsg").html("");
    	$("#addForm").find("input[name='id']").val("");
		$('#addForm').find("input[name='freightName']").val("");
		$('#addForm').find("select[name='firstHeavy']").val(""); 
		$('#addForm').find("input[name='firstHeavyFreight']").val("");
		$('#addForm').find("input[name='otherHeavyFreight']").val("");
		$('#addForm').find("input[name='remark']").val("");
		$('#addForm').find("select[name='warehouseId']").val("");
    	loadOptDetail(id); 
});

/** 修改start **/
$("#btn_update").click(function() {
 	var isValid = updateForm.valid();
     if (isValid) { 	
     	 $.post(ctx+"/freight/update", $('#updateForm').serialize(),function (data) {
     		var result = data.result;
			if(result!=null && result=="noauthority") {
				common.alert1('你无此权限!');
				return;
			}
     		 if(result!=null && result=="againLogin") {
    			 $('.commonConfirm').modal('hide');
    			 common.alert1("登录过期，请重新登录");
				 setTimeout("location.href = ctx + '/login.html'",1000);
             }else if(result=='success'){ 
          		 $(".modal-edit").modal('hide');
      	         common.mySuccessModal2('修改成功!');
      	       showByPage(1);
          	}else{
          		$(".updateMsg").html(data.message);
          	}
          },"json"); 
     }  
});
/** 修改end **/
	
	 
	
	function isnull(value) {
		if (value == null || value == '' || typeof (value) == "undefined")
			return "--";
		else
			return value;
	}
	
	
	/*类型，1发货区域，2停发区域*/
	function freightType(value) {
		if (value == null || value == ''){
			return "未知";
		}else if(value == 1) {
			return "<span style='color:blue'>发货区域</span>";
		}else if(value == 2) {
			return "<span style='color:red'>停发区域</span>";
		} 
	}

	/*状态，1启用，2禁用*/
	function status(value) {
		if (value == null || value == ''){
			return "未知";
		}else if(value == 1) {
			return "启用";
		}else if(value == 2) {
			return "停用";
		} 
	}
	
	function isnumber(value) {
		if (value == null || value == '')
			return "0.00";
		else
			return value;
	}
	
});

function removeImg(t) {
	var o=document.getElementById("resultupdate");//获取父节点
	var a=document.getElementById(t);//获取需要删除的子节点
	o.removeChild(a)//从父节点o上面移除子节点a
	var ids = $("#updateImgIds").val() + t+",";
	$("#updateImgIds").val(ids);
} 
/*e*/

